<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>深度优先遍历</title>
</head>
<body>
	<script>
	/*
		递归实现，写法简单，时间复杂度为O(n²)
	 */
	function deepTree(tree, arr = []) {
		tree.forEach(data => {
			arr.push(data.id);
			// 遍历子树
			data.children && deepTree(data.children, arr);
		});
		return arr;
	}

	let tree = [
		{
			id: "1",
			title: "节点 1",
			children: [
				{
					id: "1-1",
					title: "节点 1-1"
				},
				{
					id: "1-2",
					title: "节点 1-2"
				}
			]
		},
		{
			id: "2",
			title: "节点 2",
			children: [
				{
					id: "2-1",
					title: "节点 2-1"
				}
			]
		}
	];
	console.log(deepTree(tree)); // ['1', '1-1', '1-2', '2', '2-1']
	</script>
</body>
</html>`